<template>
  <view v-if="visible" class="warning-popup">
    <view class="warning-content">
      <view class="warning-icon">⚠️</view>
      <view class="warning-title">碰撞警告</view>
      <view class="warning-message">{{ message }}</view>
      <view class="warning-timer">{{ countdown }}秒后自动关闭</view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'WarningPopup',
  props: {
    message: {
      type: String,
      default: '检测到碰撞风险！'
    },
    duration: {
      type: Number,
      default: 5
    }
  },
  data() {
    return {
      visible: false,
      countdown: 5,
      timer: null
    }
  },
  methods: {
    show() {
      this.visible = true;
      this.countdown = this.duration;
      this.startCountdown();
    },
    hide() {
      this.visible = false;
      if (this.timer) {
        clearInterval(this.timer);
        this.timer = null;
      }
    },
    startCountdown() {
      if (this.timer) {
        clearInterval(this.timer);
      }
      
      this.timer = setInterval(() => {
        this.countdown--;
        
        if (this.countdown <= 0) {
          this.hide();
        }
      }, 1000);
    }
  },
  beforeDestroy() {
    if (this.timer) {
      clearInterval(this.timer);
    }
  }
}
</script>

<style scoped>
.warning-popup {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(255, 0, 0, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}

.warning-content {
  background-color: rgba(0, 0, 0, 0.85);
  border-radius: 16rpx;
  width: 80%;
  max-width: 600rpx;
  padding: 40rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.warning-icon {
  font-size: 80rpx;
  margin-bottom: 20rpx;
}

.warning-title {
  color: #ff4d4f;
  font-size: 36rpx;
  font-weight: bold;
  margin-bottom: 20rpx;
}

.warning-message {
  color: white;
  font-size: 30rpx;
  text-align: center;
  margin-bottom: 30rpx;
}

.warning-timer {
  color: #aaa;
  font-size: 24rpx;
}
</style> 